﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SPVisualDev.VSAddin.Dialogs
{
    public partial class DialogImportSPDlls : Form
    {
        private DataTable m_data_table;

        public DialogImportSPDlls()
        {
            InitializeComponent();

            this.dgvReferencedAssemblies.AllowUserToAddRows = false;
            this.dgvReferencedAssemblies.AllowUserToDeleteRows = false;
            this.dgvReferencedAssemblies.AllowUserToOrderColumns = false;
            this.dgvReferencedAssemblies.AllowUserToResizeRows = false;

            this.dgvReferencedAssemblies.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dgvReferencedAssemblies.RowHeadersVisible = false;
            this.dgvReferencedAssemblies.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dgvReferencedAssemblies.ShowEditingIcon = false;
            this.dgvReferencedAssemblies.AllowUserToOrderColumns = false;
            this.dgvReferencedAssemblies.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.dgvReferencedAssemblies.BackgroundColor = Color.White;
            this.dgvReferencedAssemblies.RowsDefaultCellStyle.SelectionBackColor = Color.White;
            this.dgvReferencedAssemblies.RowsDefaultCellStyle.SelectionForeColor = Color.Black;
        }

        public struct ImportSPDllsStruct
        {
            public string Name;
            public bool Import;

            public ImportSPDllsStruct(string Name, bool Import)
            {
                this.Name = Name;
                this.Import = Import;
            }
        }

        public List<ImportSPDllsStruct> Assemblies
        {
            get
            {
                List<ImportSPDllsStruct> ret = new List<ImportSPDllsStruct>();

                foreach (DataRow row in m_data_table.Rows)
                {
                    string name = row.ItemArray[0] as string;
                    bool opt1 = (bool)row.ItemArray[1];

                    if (opt1)
                    {
                        ImportSPDllsStruct asm = new ImportSPDllsStruct(name, opt1);

                        ret.Add(asm);
                    }
                }

                return ret;
            }
            set
            {
                List<ImportSPDllsStruct> list = value;
                if (value == null)
                {
                    list = new List<ImportSPDllsStruct>();
                }

                FillDatagrid(list);
            }
        }

        public string InfoMessage
        {
            get { return this.lblTopic.Text; }
            set { this.lblTopic.Text = value; }
        }

        private void FillDatagrid(List<ImportSPDllsStruct> listOfIncludedAssemblies)
        {
            m_data_table = new DataTable();

            m_data_table.Columns.Add("Assembly");
            m_data_table.Columns.Add("Import");

            m_data_table.Columns["Assembly"].DataType = typeof(string);
            m_data_table.Columns["Assembly"].ReadOnly = true;
            m_data_table.Columns["Import"].DataType = typeof(bool);

            foreach (ImportSPDllsStruct asm in listOfIncludedAssemblies)
            {
                m_data_table.Rows.Add(asm.Name, asm.Import);
            }

            DataView view = new DataView(m_data_table);
            view.Sort = "Assembly";

            this.dgvReferencedAssemblies.DataSource = view;

            this.dgvReferencedAssemblies.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            this.dgvReferencedAssemblies.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Hide();
        }
    }
}
